package com.wl156.activities.messages;

import java.io.IOException;

import org.xmlpull.v1.XmlPullParserException;

import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.wl156.BaseActivity;
import com.wl156.MyApp;
import com.wl156.R;
import com.wl156.model.Certificate;
import com.wl156.model.WebServiceClient;
import com.wl156.widget.ProviceSelector;

public class AddFeedInternal extends BaseActivity implements OnClickListener {

	private ProviceSelector mFromProviceSelector;
	private ProviceSelector mToProviceSelector;
	private RadioGroup mTypeGroup;
	private Button mAdd;
	private WebServiceClient mWebServiceClient;
	private Certificate mCertificate;

	private boolean mUpdate = false;
	private long subscribeID;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

//		Window win = getWindow();
//		win.requestFeature(Window.FEATURE_LEFT_ICON);
		setContentView(R.layout.act_add_feed_internal);
//		win.setFeatureDrawableResource(Window.FEATURE_LEFT_ICON,
//				R.drawable.ic_launcher);

		mUpdate = getIntent().getBooleanExtra("update", false);
		if (mUpdate) {
			subscribeID = getIntent().getLongExtra("subscribeID", 0);
		}

		mWebServiceClient = new WebServiceClient(this);
		mCertificate = ((MyApp) getApplication()).getCertificateFromXml();

		mFromProviceSelector = (ProviceSelector) findViewById(R.id.provice_selector);
		mFromProviceSelector.setTitle("起点: ");
		mFromProviceSelector.setProviceRequried(true);
		mFromProviceSelector.setCityRequried(true);
		mFromProviceSelector.hideRegion(true);
		mToProviceSelector = (ProviceSelector) findViewById(R.id.to_provice_selector);
		mToProviceSelector.setTitle("终点: ");
		mToProviceSelector.setProviceRequried(true);
		mToProviceSelector.setCityRequried(true);
		mToProviceSelector.hideRegion(true);
		mTypeGroup = (RadioGroup) findViewById(R.id.type);
		// mTypeGroup.getCheckedRadioButtonId();
		mAdd = (Button) findViewById(R.id.add);
		mAdd.setOnClickListener(this);

		if (mUpdate) {
			mAdd.setText("更新");
		} else {
			mAdd.setText("添加");
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.add:
			if (TextUtils.isEmpty(mFromProviceSelector.getProvice())
					|| TextUtils.isEmpty(mFromProviceSelector.getCity())
					|| TextUtils.isEmpty(mToProviceSelector.getProvice())
					|| TextUtils.isEmpty(mToProviceSelector.getCity())) {
				Toast.makeText(this, "请填写相应字段", Toast.LENGTH_SHORT).show();
				return;
			}

			new AsyncTask<Void, Void, Object>() {
				ProgressDialog mDialog;

				@Override
				protected void onPreExecute() {
					mDialog = ProgressDialog.show(AddFeedInternal.this, "",
							"添加中...");
				};

				@Override
				protected void onPostExecute(Object result) {
					mDialog.dismiss();

					if (result instanceof Exception) {
						Toast.makeText(AddFeedInternal.this, "添加失败，请稍后再试",
								Toast.LENGTH_SHORT).show();
					} else {
						Toast.makeText(AddFeedInternal.this, "添加成功",
								Toast.LENGTH_SHORT).show();
					}
				};

				@Override
				protected Object doInBackground(Void... params) {
					try {
						if (mUpdate) {
							return mWebServiceClient
									.common_modifySubscribe(
											mCertificate,
											subscribeID,
											mFromProviceSelector.getProvice(),
											mFromProviceSelector.getCity(),
											mToProviceSelector.getProvice(),
											mToProviceSelector.getCity(),
											mTypeGroup
													.getCheckedRadioButtonId() == R.id.goodMessage ? "2"
													: "1");
						} else {
							return mWebServiceClient
									.common_addSubscribe(
											mCertificate,
											mFromProviceSelector.getProvice(),
											mFromProviceSelector.getCity(),
											mToProviceSelector.getProvice(),
											mToProviceSelector.getCity(),
											mTypeGroup
													.getCheckedRadioButtonId() == R.id.goodMessage ? "2"
													: "1");
						}
					} catch (IOException e) {
						e.printStackTrace();
						return e;
					} catch (XmlPullParserException e) {
						e.printStackTrace();
						return e;
					}
				}

			}.execute();
			break;
		}
	}
}
